import { Rules } from "async-validator";
import { Context } from "koa";
import pagination from "../../utils/pagination";
import requestParams from "../../utils/requestParams";
import response from "../../utils/response";
import pageServer from "../service/pageServer";
import validate from "../../utils/validate";

/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2021-11-28 17:12:10
 * 天凉好个秋
 */
class pageController {
  // 获取页面列表
  async getPageList(ctx: Context) {
    const { pageIndex, limit } = requestParams(ctx);
    const { count, rows } = await pageServer.getPageList(
      Number(pageIndex),
      Number(limit)
    );
    response.success(ctx, pagination(rows, pageIndex, count, limit));
  }

  // 保存并使用页面
  async saveAndUse(ctx: Context) {
    const formData = requestParams(ctx);
    try {
      formData.content = JSON.parse(formData.content);
    } catch (error) {}
    const rules: Rules = {
      name: { type: "string", required: true, message: "请输入版本名" },
      content: { type: "object", required: true },
    };
    const { data, error } = await validate(ctx, rules, formData);
    if (error !== null) return response.error(ctx, error);
    try {
      const hasName = await pageServer.getPageByName(data.name);
      if (hasName) return response.error(ctx, "名称已存在");
      const { id } = await pageServer.addPage({ name: data.name, is_using: 1 });
      const d = await pageServer.addPageContent(id, data.content);
      pageServer.setActive(id);
      if (d !== null) return response.success(ctx);
      return response.error(ctx, "保存失败");
    } catch (error) {
      response.error(ctx, error);
    }
  }

  //   根据id获取页面内容
  async getPageContent(ctx: Context) {
    const id = ctx.params["id"] as number;
    const data = (await pageServer.getPageContent(id)).get({plain: true});
    const resData = {
      id: data.Id,
      name: data.name,
      content: data.content,
    };
    
    response.success(ctx, resData);
  }

  //   获取当前正在使用的页面内容
  async getCurPage(ctx: Context) {
    const data = (await pageServer.getCurPage()).get({plain: true});
    if (data !== null) {
      const resData = {
        id: data.Id,
        name: data.name,
        content: data.content.content,
      };
      response.success(ctx, resData);
    } else {
      const defaultData = (await pageServer.getPageContent("undefined")).get({plain: true});
      const resData = {
        id: defaultData.pageId,
        name: defaultData.pageInfo.name,
        content: defaultData.content,
      };
      response.success(ctx, resData);
    }
  }

  //   删除页面
  async deletePage(ctx: Context) {
    const id = ctx.params["id"] as number;
    if (id == undefined || isNaN(id)) return response.error(ctx, "id错误");
    if(id == 1) return response.error(ctx, '默认版本，不可删除')
    const [num1, num2] = await Promise.all([
      pageServer.deletePage(id),
      pageServer.deletePageContent(id),
    ]);
    if (num1 > 0 && num2 > 0) {
      response.success(ctx);
    } else {
      console.log(num1, num2);
      
      response.error(ctx, "删除失败");
    }
  }
}

export default new pageController();
